package com.aifast.system.sys.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.aifast.common.layui.LayuiDataTable;
import com.aifast.common.layui.LayuiPageFactory;
import com.aifast.system.sys.dao.SysDictDao;
import com.aifast.system.sys.entity.SysDict;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 字典类别 服务实现类
 * </p>
 *
 * @author fujian
 * @since 2021-12-29
 */
@Service
@RequiredArgsConstructor
public class SysDictService extends ServiceImpl<SysDictDao, SysDict> {

    /**
     * 获取列表数据
     *
     * @param searchValue 关键字搜索
     * @param page        分页
     * @return
     */
    public LayuiDataTable getDicts(String searchValue, Page page) {
        LambdaQueryWrapper<SysDict> wrapper = new LambdaQueryWrapper<SysDict>()
                .like(StrUtil.isNotBlank(searchValue), SysDict::getName, searchValue).or()
                .like(StrUtil.isNotBlank(searchValue), SysDict::getRemarks, searchValue).or()
                .like(StrUtil.isNotBlank(searchValue), SysDict::getCode, searchValue)
                .orderByDesc(SysDict::getId);
        Page<SysDict> list = page(page, wrapper);
        list.getRecords().forEach(i -> {
            i.setName(StrUtil.join(" ", i.getName(), i.getCode()));
        });
        return LayuiPageFactory.init(list);
    }


    /**
     * 新增或修改
     *
     * @param sysDict
     * @return
     */
    public Boolean insertOrUpdate(SysDict sysDict) {
        return saveOrUpdate(sysDict);
    }


    /**
     * 删除 和 批量删除
     *
     * @param id  数据id
     * @param ids 数据id集合
     * @return
     */
    public Boolean del(Integer id, String ids) {
        List<Integer> integers = new ArrayList<>();
        if (StrUtil.isNotBlank(ids)) {
            integers = Convert.toList(Integer.class, ids.split(","));
        }
        if (!Objects.equals(null, id)) {
            integers.add(id);
        }
        try {
            integers.forEach(i -> {
                update(new UpdateWrapper<SysDict>().set("deleted", 1).eq("id",i));
            });
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public Object getAll() {
        List<SysDict> list = list(new QueryWrapper<SysDict>().eq("state", 0).orderByDesc("sort"));
        return list.stream().map(i -> i.setName(StrUtil.join(" ", i.getName(), i.getCode())));
    }

    public List<SysDict> getByMode(int i) {
        return list(new QueryWrapper<SysDict>().eq("state", 0).eq("mode", i).orderByDesc("sort"));
    }
}
